package com.ccit.controller;


import com.ccit.domain.*;
import com.ccit.service.ApprovalService;
import com.ccit.service.AttendanceService;
import com.ccit.service.EmployeeService;
import com.ccit.utils.Code;
import com.ccit.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/analyse")
@CrossOrigin(origins = "http://localhost:8080")
@Api(description = "数据分析")
public class AnalyseController {
    @Autowired
    private ApprovalService approvalService;
    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private EmployeeService employeeService;

    @GetMapping()
    public Result getAnalyse()  {
        List<Analyse> list =  new ArrayList<>();
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        for (int i = 0; i < 7; i++) {
            // 计算日期偏移量  表示将当前日期往前推 i 天
            LocalDate date = currentDate.minusDays(i);
            // 构建日期字符串，格式为 "YYYY-MM-DD"
            String dateString = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            // 获取出勤数据
            List<AttendanceType> data = attendanceService.getAttendanceByDate(dateString);
//            System.out.println(data);
            // 当前员工总数
            List<Employee> employees = employeeService.selectAll();
            // 当前请假人数
            int approvals = employees.size() - data.size();
            // 创建 Analyse 对象并添加到 list 中
            Analyse analyse = new Analyse(dateString, employees.size(), data.size(), approvals);
            list.add(analyse);
        }
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }


}

